﻿package bakudo.engines.mapEngine {
	import bakudo.engines.mapEngine.controller.renderer.ViewRenderer;
	import bakudo.engines.mapEngine.events.CameraEvent;
	import bakudo.engines.mapEngine.model.actors.ModelActor;
	import bakudo.engines.mapEngine.model.WorldModel;
	import bakudo.engines.mapEngine.view.scene.Scene2D;	
	import bakudo.engines.mapEngine.controller.camera.Camera2D;	
	import flash.geom.Point;

	/**
	 * @author Micha� Rabi�ski
	 */
	public class BakudoMapEngine {

		
		/** Kontroluje widok */
		public var camera2D : Camera2D;
		
		/** Miejsce w, ktorym renderujemy widok */
		public var scene2D : Scene2D;
		
		/** Kontroler wyswietlania grafiki */
		public var viewRenderer : ViewRenderer;
		
		/** Model mapy */
		public var worldModel : WorldModel;
		
		
		
	//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	// PUBLIC METHODS
	//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	
		/**
		 * podlacza potrzebne klasie elementy
		 * @param scene - scena ktora poruszamy
		 */
		public function connect(scene : Scene2D,gridSize : uint) : void {
			// model
			worldModel = new WorldModel();
			worldModel.gridSize = gridSize;
			worldModel.viewWidth = 800;
			worldModel.viewHeight = 600;
			worldModel.gridViewDistance = Math.ceil(worldModel.viewWidth / worldModel.gridSize/2);
			trace(worldModel.gridViewDistance);
			
			// controller
			viewRenderer = new ViewRenderer(this);
			camera2D = new Camera2D(this);
			camera2D.smooth = true;
			
			// view
			scene2D = scene;
			scene2D.worldModel = worldModel;
			addListeners();
			trace('::::::\tBAKUDO MAP ENGINE CONNECTED\t::::::');
			trace('\n\tgridCellSize : ' + worldModel.gridSize);
			trace('\n\tviewWidth : ' + worldModel.viewWidth);
			trace('\n\tviewHeight : ' + worldModel.viewHeight);
			trace('\n\tgridViewDistance : ' + worldModel.gridViewDistance);
		}
		
		/**
		 * Ustawia komorki tla w siatce
		 * @param	tiles - tablica z aktorami
		 * @param	hTilesCount - ilosc kolumn w siatce 
		 */
		public function setTiles(tiles : Array/*of ModelActor*/,hTilesCount : uint):void {
			worldModel.buildBackgroundGrid(tiles.length, 116, 100);
			worldModel.addAndSortBackgroundTiles(tiles);
			scene2D.setup();
		}
		
		public function setCharacters(characters:Array/*of ModelActor*/):void {
			worldModel.addCharacters(characters);
		}
		
		public function setObstacles(obstacles : Array/*of ModelActor*/):void {
			worldModel.addObstacles(obstacles);
		}
		
		public function centerToObject(actor : ModelActor):void {
			camera2D.centerToObject(actor);
		}

		
		
	//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	// PRIVATE METHODS
	//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	
		// dodaje listenery do potrzebnych elementow
		private function addListeners():void {
			camera2D.addEventListener(CameraEvent.CAMERA_UPDATE, onCameraUpdate, false, 0, true);
		}
		
		
		
	//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	// PRIVATE LISTENERS
	//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	
		// handler zmiany pozycji kamery, wywoluje metode kontrolera grafiki
		// odswierzajaca widok
		private function onCameraUpdate(e:CameraEvent):void {
			viewRenderer.doRenderView();
		}
		
	}
}
